﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CarLoanSystem.Entity;

namespace CarLoanSystem.DAL
{
    public class CarInfoDAL : DBBase
    {
        public static DataTable LoadAllCar(string id = "", string brandName = "", string modelName = "")
        {
            try
            {
                string sqlQuery = @"SELECT dbo.CAR_BRAND.CAR_BRAND_NAME, dbo.CAR_MODEL.CAR_MODEL_NAME, dbo.CAR_INFO.CAR_ID, dbo.CAR_INFO.CAR_TYPE_ENGINE, 
                                    dbo.CAR_INFO.CAR_DETAIL, dbo.CAR_INFO.CAR_PRICE, dbo.CAR_INFO.CAR_BRAND_ID, dbo.CAR_INFO.CAR_MODEL_ID, dbo.CAR_INFO.CAR_PATH_IMAGE
                                    FROM dbo.CAR_BRAND INNER JOIN
                                    dbo.CAR_INFO ON dbo.CAR_BRAND.CAR_BRAND_ID = dbo.CAR_INFO.CAR_BRAND_ID INNER JOIN
                                    dbo.CAR_MODEL ON dbo.CAR_BRAND.CAR_BRAND_ID = dbo.CAR_MODEL.CAR_BRAND_ID AND dbo.CAR_INFO.CAR_MODEL_ID = dbo.CAR_MODEL.CAR_MODEL_ID where 1=1 ";

                if (id != "")
                {
                    sqlQuery += "and dbo.CAR_INFO.CAR_ID = " + id;
                }
                if (brandName != "")
                {
                    sqlQuery += "and dbo.CAR_BRAND.CAR_BRAND_NAME like '%" + brandName + "%'";
                }
                if (modelName != "")
                {
                    sqlQuery += "and dbo.CAR_MODEL.CAR_MODEL_NAME like '%" + modelName + "%'";
                }

                DBBase.ConncetDatabase();

                SqlCommand command = new SqlCommand(sqlQuery, DBBase.con);
                SqlDataReader dr = command.ExecuteReader();

                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                    DBBase.DisConncetDatabase();
                    return dt;
                }
                else
                {
                    DBBase.DisConncetDatabase();
                    return null;
                }

            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static DataTable LoadAllCarForWeb(string brandId = "", string modelId = "")
        {
            try
            {
                string sqlQuery = @"SELECT dbo.CAR_BRAND.CAR_BRAND_NAME, dbo.CAR_MODEL.CAR_MODEL_NAME, dbo.CAR_INFO.CAR_ID, dbo.CAR_INFO.CAR_TYPE_ENGINE, 
                                    dbo.CAR_INFO.CAR_DETAIL, dbo.CAR_INFO.CAR_PRICE, dbo.CAR_INFO.CAR_BRAND_ID, dbo.CAR_INFO.CAR_MODEL_ID, dbo.CAR_INFO.CAR_PATH_IMAGE
                                    FROM dbo.CAR_BRAND INNER JOIN
                                    dbo.CAR_INFO ON dbo.CAR_BRAND.CAR_BRAND_ID = dbo.CAR_INFO.CAR_BRAND_ID INNER JOIN
                                    dbo.CAR_MODEL ON dbo.CAR_BRAND.CAR_BRAND_ID = dbo.CAR_MODEL.CAR_BRAND_ID AND dbo.CAR_INFO.CAR_MODEL_ID = dbo.CAR_MODEL.CAR_MODEL_ID where 1=1 ";

                if (brandId != "")
                {
                    sqlQuery += "and dbo.CAR_BRAND.CAR_BRAND_ID = " + brandId;
                }
                if (modelId != "")
                {
                    sqlQuery += "and dbo.CAR_MODEL.CAR_MODEL_ID = " + modelId;
                }

                DBBase.ConncetDatabase();

                SqlCommand command = new SqlCommand(sqlQuery, DBBase.con);
                SqlDataReader dr = command.ExecuteReader();

                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                    DBBase.DisConncetDatabase();
                    return dt;
                }
                else
                {
                    DBBase.DisConncetDatabase();
                    return null;
                }

            }
            catch (Exception ex)
            {
                return null;
            }
        }


        public static CarInfoItem LoadCar(int id)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlDataReader dr;
                SqlCommand command = new SqlCommand(@"SELECT dbo.CAR_BRAND.CAR_BRAND_NAME, dbo.CAR_MODEL.CAR_MODEL_NAME, dbo.CAR_INFO.CAR_ID, dbo.CAR_INFO.CAR_TYPE_ENGINE, 
                                    dbo.CAR_INFO.CAR_DETAIL, dbo.CAR_INFO.CAR_PRICE, dbo.CAR_INFO.CAR_BRAND_ID, dbo.CAR_INFO.CAR_MODEL_ID, dbo.CAR_INFO.CAR_PATH_IMAGE
                                    FROM dbo.CAR_BRAND INNER JOIN
                                    dbo.CAR_INFO ON dbo.CAR_BRAND.CAR_BRAND_ID = dbo.CAR_INFO.CAR_BRAND_ID INNER JOIN
                                    dbo.CAR_MODEL ON dbo.CAR_BRAND.CAR_BRAND_ID = dbo.CAR_MODEL.CAR_BRAND_ID AND dbo.CAR_INFO.CAR_MODEL_ID = dbo.CAR_MODEL.CAR_MODEL_ID 
                                    where [CAR_ID] = @CAR_ID ", DBBase.con);
                command.Parameters.AddWithValue("@CAR_ID", id);
                dr = command.ExecuteReader();
                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                }
                DBBase.DisConncetDatabase();
                if (dt.Rows.Count > 0)
                {
                    CarInfoItem item = new CarInfoItem();
                    item.CAR_BRAND_ID = Convert.ToInt32(dt.Rows[0]["CAR_BRAND_ID"].ToString());
                    item.CAR_BRAND_NAME = dt.Rows[0]["CAR_BRAND_NAME"].ToString();
                    item.CAR_MODEL_ID = Convert.ToInt32(dt.Rows[0]["CAR_MODEL_ID"].ToString());
                    item.CAR_MODEL_NAME = dt.Rows[0]["CAR_MODEL_NAME"].ToString();
                    item.CAR_ID = Convert.ToInt32(dt.Rows[0]["CAR_ID"].ToString());
                    item.CAR_PATH_IMAGE = dt.Rows[0]["CAR_PATH_IMAGE"].ToString();
                    item.CAR_PRICE = Convert.ToDecimal(dt.Rows[0]["CAR_PRICE"].ToString());
                    item.CAR_TYPE_ENGINE = dt.Rows[0]["CAR_TYPE_ENGINE"].ToString();
                    item.CAR_DETAIL = dt.Rows[0]["CAR_DETAIL"].ToString();
                    return item;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static CarInfoItem LoadCarID(int brandId,int modelId,string typeEngine)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlDataReader dr;
                SqlCommand command = new SqlCommand(@"SELECT [CAR_ID] FROM [CAR_LOAN].[dbo].[CAR_INFO]
                                    where [CAR_BRAND_ID] = @CAR_BRAND_ID and [CAR_MODEL_ID] = @CAR_MODEL_ID 
                                    and [CAR_TYPE_ENGINE] = @CAR_TYPE_ENGINE", DBBase.con);
                command.Parameters.AddWithValue("@CAR_BRAND_ID", brandId);
                command.Parameters.AddWithValue("@CAR_MODEL_ID", modelId);
                command.Parameters.AddWithValue("@CAR_TYPE_ENGINE", typeEngine);
                dr = command.ExecuteReader();
                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                }
                DBBase.DisConncetDatabase();
                if (dt.Rows.Count > 0)
                {
                    CarInfoItem item = new CarInfoItem();
                    item.CAR_ID = Convert.ToInt32(dt.Rows[0]["CAR_ID"].ToString());
                    return item;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static void Update(CarInfoItem item)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlCommand command = new SqlCommand(@"UPDATE [CAR_LOAN].[dbo].[CAR_INFO]
                                               SET [CAR_PRICE] = @CAR_PRICE
                                                  ,[CAR_DETAIL] = @CAR_DETAIL
                                                  ,[CAR_PATH_IMAGE] = @CAR_PATH_IMAGE
                                             WHERE ([CAR_ID] = @CAR_ID)", DBBase.con);
                command.Parameters.AddWithValue("@CAR_ID", item.CAR_ID);
                command.Parameters.AddWithValue("@CAR_PRICE", item.CAR_PRICE);
                command.Parameters.AddWithValue("@CAR_DETAIL", item.CAR_DETAIL);
                command.Parameters.AddWithValue("@CAR_PATH_IMAGE", item.CAR_PATH_IMAGE);
                int tmp = command.ExecuteNonQuery();
                DBBase.DisConncetDatabase();
            }
            catch (Exception ex)
            {
                DBBase.DisConncetDatabase();
                throw ex;
            }
        }

        public static void Insert(CarInfoItem item)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlCommand command = new SqlCommand(@"INSERT INTO [CAR_LOAN].[dbo].[CAR_INFO]
                                                       ([CAR_TYPE_ENGINE]
                                                       ,[CAR_DETAIL]
                                                       ,[CAR_PRICE]
                                                       ,[CAR_BRAND_ID]
                                                       ,[CAR_MODEL_ID]
                                                       ,[CAR_PATH_IMAGE])
                                                 VALUES
                                                      (@CAR_TYPE_ENGINE
                                                       ,@CAR_DETAIL
                                                       ,@CAR_PRICE
                                                       ,@CAR_BRAND_ID
                                                       ,@CAR_MODEL_ID
                                                       ,@CAR_PATH_IMAGE)", DBBase.con);
                command.Parameters.AddWithValue("@CAR_TYPE_ENGINE", item.CAR_TYPE_ENGINE);
                command.Parameters.AddWithValue("@CAR_DETAIL", item.CAR_DETAIL);
                command.Parameters.AddWithValue("@CAR_PRICE", item.CAR_PRICE);
                command.Parameters.AddWithValue("@CAR_BRAND_ID", item.CAR_BRAND_ID);
                command.Parameters.AddWithValue("@CAR_MODEL_ID", item.CAR_MODEL_ID);
                command.Parameters.AddWithValue("@CAR_PATH_IMAGE", item.CAR_PATH_IMAGE);
                int tmp = command.ExecuteNonQuery();
                DBBase.DisConncetDatabase();
            }
            catch (Exception ex)
            {
                DBBase.DisConncetDatabase();
                throw ex;
            }
        }
    }
}